//
//  PlanningTopicCoreTextLabel.m
//  testLineHieght
//
//  Created by tianhy on 13-9-10.
//  Copyright (c) 2013年 tianhy. All rights reserved.
//

#import "PlanningTopicCoreTextLabel.h"
#import <CoreText/CoreText.h>

@implementation PlanningTopicCoreTextLabel
@synthesize lineSpace;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _allowIndent = NO;
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
  UIFont *font = self.font;
  NSString *text = self.text;
  
  //字体
  CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef) font.fontName, font.pointSize, NULL);
  
  //行高
  float _lineHeight = lineSpace;
  CTParagraphStyleSetting lineSpaceSetting;
  lineSpaceSetting.spec = kCTParagraphStyleSpecifierLineSpacing;
  lineSpaceSetting.value = &_lineHeight;
  lineSpaceSetting.valueSize = sizeof(CGFloat);
  
  //对齐方式
  CTParagraphStyleSetting alignmentStyleSetting;
  CTTextAlignment alignment = kCTJustifiedTextAlignment;
  alignmentStyleSetting.spec=kCTParagraphStyleSpecifierAlignment;
  alignmentStyleSetting.valueSize=sizeof(alignment);
  alignmentStyleSetting.value=&alignment;
  
    // 行间距
  CGFloat leading = font.lineHeight -font.ascender + font.descender;
  CTParagraphStyleSetting leadSetting;
  leadSetting.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
  leadSetting.value = &leading;
  leadSetting.valueSize = sizeof(float);
    
    CTParagraphStyleRef paragraphStyle;
    if (_allowIndent) {
        // 首行缩进
        CTParagraphStyleSetting fristline;
        CGFloat fristlineindent = 2.0f*(font.pointSize)+1;
        fristline.spec = kCTParagraphStyleSpecifierFirstLineHeadIndent;
        fristline.value = &fristlineindent;
        fristline.valueSize = sizeof(float);
        CTParagraphStyleSetting paragraphSettings[4] = {fristline,lineSpaceSetting,alignmentStyleSetting,leading};
        paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 4);
    }else{
        CTParagraphStyleSetting paragraphSettings[3] = {lineSpaceSetting,alignmentStyleSetting,leading};
        paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 3);
    }
    
  
  
  
  
  CFRange textRange = CFRangeMake(0, text.length);
  if(!text)
      text = @" ";
  CFMutableAttributedStringRef string = CFAttributedStringCreateMutable(kCFAllocatorDefault, text.length);
  CFAttributedStringReplaceString(string, CFRangeMake(0, 0), (CFStringRef)text);
  
  CFAttributedStringSetAttribute(string, textRange, kCTFontAttributeName, fontRef);
  CFAttributedStringSetAttribute(string, textRange, kCTParagraphStyleAttributeName, paragraphStyle);
  //字体颜色
  CFAttributedStringSetAttribute(string, textRange, kCTForegroundColorAttributeName, self.textColor.CGColor);
  
  CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(string);

  CGMutablePathRef path=CGPathCreateMutable();
  CGPathAddRect(path, NULL, self.bounds);
  CTFrameRef frameRef = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, [text length]), path, NULL);
  CGContextRef context=UIGraphicsGetCurrentContext();
  CGContextSetTextMatrix(context, CGAffineTransformIdentity);
  CGContextSaveGState(context);
  CGContextTranslateCTM(context, 0, self.bounds.size.height);
  CGContextScaleCTM(context, 1.0, -1.0);
  
  CTFrameDraw(frameRef, context);
  CGContextRestoreGState(context);
  
  CFRelease(fontRef);
  CFRelease(paragraphStyle);
  CFRelease(string);
  CFRelease(frameSetter);
  CFRelease(path);
  CFRelease(frameRef);
}


@end
